# !/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
Time      : 2019/8/19 12:02
@Author   : gao
@Site     : 
@File     : write_data2file_thread.py
@Software : PyCharm
"""
import csv
from PySide6.QtCore import *
from my_log.logger import logger
from utils.write_data_2_file import writeData2File

'''由于数据的写入属于耗时的IO操作，所以将这种操作放到单独的线程中进行'''


class Signal(QObject):
    finishSignal = Signal(int)


class SaveDataToFile(QRunnable):
    def __init__(self, file_path, data_list):
        super(SaveDataToFile, self).__init__()
        self.signal = Signal()
        self.file_path = file_path
        self.data_list = data_list
        self.flag = True

    def run(self):
        try:
            if self.flag:
                with open(self.file_path, 'a+', newline='', encoding="utf_8_sig") as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(self.data_list)
        except Exception as e:
            logger.error("SaveDataToFile," + str(e))

    def cancel_thread(self):
        self.flag = False
